package com.lyh.hkcloud.util.http;

import android.content.Context;
import android.util.Log;
import com.loopj.android.http.RequestParams;
import com.lyh.hkcloud.constants.AssetManagementConstants;
import com.lyh.hkcloud.constants.UrlConstants;
import com.lyh.hkcloud.constants.UserInfo;
import com.lyh.hkcloud.util.base.ConvertUtil;
import com.lyh.hkcloud.util.base.JsonUtil;
import com.lyh.hkcloud.util.base.SaveData;
import com.lyh.hkcloud.util.base.StringUtils;
import com.lyh.hkcloud.util.http.listener.handler.JsonHttpResponseHandlerImpl;
import com.lyh.hkcloud.util.http.listener.handler.TextHttpResponseHandlerImpl;
import com.lyh.hkcloud.util.http.listener.json.JsonFailuredListener;
import com.lyh.hkcloud.util.http.listener.json.JsonSuccessListener;
import com.lyh.hkcloud.util.http.listener.text.TextFailuredListener;
import com.lyh.hkcloud.util.http.listener.text.TextSuccessListener;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class Request {

    public static void post(Context context, String url, Map<String, String> headMap, JSONObject jsonObject, final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        assert (successListener != null);
        assert (failuredListener != null);

        HttptRequest.post(context, url, headMap, jsonObject, new JsonHttpResponseHandlerImpl(successListener, failuredListener, context));
    }

    public static void post(Context context, String url, JSONObject jsonObject, final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        assert (successListener != null);
        assert (failuredListener != null);
        Map<String, String> headMap = new HashMap<>();
        if (ConvertUtil.isNotEmpty(UserInfo.keyId)) {
            headMap.put("keyId", UserInfo.keyId);
        }

        Log.v("SaveData.token\"):",SaveData.getString(context,"token"));
        headMap.put("user-agent", "Android");

        if (StringUtils.isNotEmpty(SaveData.getString(context,"token"))) {
            headMap.put("authorization", SaveData.getString(context,"token"));
        }

        JsonUtil.put(jsonObject, "deviceNo", UserInfo.deviceNo);
        JsonUtil.put(jsonObject, "version", "");
        //资产管理系统的商户id
        JsonUtil.put(jsonObject, "merchantId", AssetManagementConstants.PRODUCTION_MERCHANT_ID);

        if (!(url + jsonObject.toString()).equals(UrlConstants.key)) {
            UrlConstants.version = System.currentTimeMillis();
            UrlConstants.key = url + jsonObject.toString();
        }

//        JsonUtil.put(jsonObject, "version", UrlConstants.version);

        HttptRequest.post(context, url, headMap, jsonObject, new JsonHttpResponseHandlerImpl(successListener, failuredListener, context));
    }

    public static void post(Context context, String url, JSONArray jsonArray, final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        assert (successListener != null);
        assert (failuredListener != null);
        assert (jsonArray != null);
        Map<String, String> headMap = new HashMap<>();
        if (ConvertUtil.isNotEmpty(UserInfo.keyId)) {
            headMap.put("keyId", UserInfo.keyId);
        }
        headMap.put("user-agent", "Android");
        int length = jsonArray.length();
        for (int i = 0; i < length; i++) {
            JSONObject jsonObject = jsonArray.optJSONObject(i);
            JsonUtil.put(jsonObject, "deviceNo", UserInfo.deviceNo);
        }

        HttptRequest.post(context, url, headMap, jsonArray, new JsonHttpResponseHandlerImpl(successListener, failuredListener, context));
    }

    public static void post(String url, RequestParams params, final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        assert (successListener != null);
        assert (failuredListener != null);

        HttptRequest.post(url, params, new JsonHttpResponseHandlerImpl(successListener, failuredListener, null));
    }

    public static void post(String url, RequestParams params, final TextSuccessListener successListener, final TextFailuredListener failuredListener) {
        assert (successListener != null);
        assert (failuredListener != null);

        HttptRequest.post(url, params, new TextHttpResponseHandlerImpl(successListener, failuredListener));
    }

}
